package com.qipay.mapper;

import com.qipay.baas.model.SpChannelRule;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

/**
 * com.qipay.mapper
 *
 * @author Administrator Ansen
 * @date 2017/12/27
 */
@Mapper
public interface SpChannelRuleMapper extends com.qipay.baas.mapper.SpChannelRuleMapper{
    @Update({
            "UPDATE `sp_channel_rule`",
            "SET `current_total_amount`=`max_total_amount`,",
            "`total_amount_last_time` = CURDATE()",
            "WHERE `total_amount_last_time` < CURDATE()"
    })
    int resetCurrentTotalAmount();

    @SelectProvider(type=SpChannelRuleSqlProvider.class, method="countByCondition")
    long countByCondition(Map<String, Object> params);


    @SelectProvider(type=SpChannelRuleSqlProvider.class, method="selectByCondition")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="sp_id", property="spId", jdbcType=JdbcType.BIGINT),
            @Result(column="channel_type", property="channelType", jdbcType=JdbcType.TINYINT),
            @Result(column="max_total_amount", property="maxTotalAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="current_total_amount", property="currentTotalAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="total_amount_last_time", property="totalAmountLastTime", jdbcType=JdbcType.DATE),
            @Result(column="max_order_amount", property="maxOrderAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="min_order_amount", property="minOrderAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="min_time_stop", property="minTimeStop", jdbcType=JdbcType.TIME),
            @Result(column="min_time_stop_duration", property="minTimeStopDuration", jdbcType=JdbcType.TIME),
            @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)

    })
    List<SpChannelRule> selectByCondition(Map<String, Object> params);

    @SelectProvider(type=SpChannelRuleSqlProvider.class, method="getBySpId")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="sp_id", property="spId", jdbcType=JdbcType.BIGINT),
            @Result(column="channel_type", property="channelType", jdbcType=JdbcType.TINYINT),
            @Result(column="max_total_amount", property="maxTotalAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="current_total_amount", property="currentTotalAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="total_amount_last_time", property="totalAmountLastTime", jdbcType=JdbcType.DATE),
            @Result(column="max_order_amount", property="maxOrderAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="min_order_amount", property="minOrderAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="min_time_stop", property="minTimeStop", jdbcType=JdbcType.TIME),
            @Result(column="min_time_stop_duration", property="minTimeStopDuration", jdbcType=JdbcType.TIME),
            @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)

    })
    List<SpChannelRule> getBySpId(Map<String, Object> params);



    @Select({
            "select *",
            "from sp_channel_rule",
            "where channel_type = #{channelType,jdbcType=INTEGER} ",
            "and `sp_id`=#{spid,jdbcType=BIGINT} limit 1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="sp_id", property="spId", jdbcType=JdbcType.BIGINT),
            @Result(column="channel_type", property="channelType", jdbcType=JdbcType.TINYINT),
            @Result(column="max_total_amount", property="maxTotalAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="current_total_amount", property="currentTotalAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="total_amount_last_time", property="totalAmountLastTime", jdbcType=JdbcType.DATE),
            @Result(column="max_order_amount", property="maxOrderAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="min_order_amount", property="minOrderAmount", jdbcType=JdbcType.BIGINT),
            @Result(column="min_time_stop", property="minTimeStop", jdbcType=JdbcType.TIME),
            @Result(column="min_time_stop_duration", property="minTimeStopDuration", jdbcType=JdbcType.TIME),
            @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    SpChannelRule selectByChannelTypeAndSpid(@Param("channelType") Byte channelType,
                                         @Param("spid") Long sp_id);
}
